home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 268 < prev    next >
Text File  |  1996-08-06  |  3KB  |  68 lines

  1. Newsgroups: comp.lang.c,comp.std.c
  2. Path: blackbush.xlink.net!slsv6bt!slsv6bt!kanze
  3. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  4. Subject: Re: Integral conversion e.t.c. (was: Re: Hungarian notation)
  5. In-Reply-To: Lawrence Kirby's message of Tue, 30 Jan 96 00:13:24 GMT
  6. Message-ID: <KANZE.96Feb1143506@slsvewt.lts.sel.alcatel.de>
  7. Sender: news@lts.sel.alcatel.de
  8. Organization: SEL
  9. References: <30C40F77.53B5@swsbbs.com> <SPENCER.96Jan22113215@zorgon.ERA.COM>
  10.     <KANZE.96Jan29201312@slsvewt.lts.sel.alcatel.de>
  11.     <822960804snz@genesis.demon.co.uk>
  12. Date: 01 Feb 1996 13:35:06 GMT
  13.  
  14. In article <822960804snz@genesis.demon.co.uk> Lawrence Kirby
  15. <fred@genesis.demon.co.uk> writes:
  16.  
  17. |> In article <KANZE.96Jan29201312@slsvewt.lts.sel.alcatel.de>
  18. |>            kanze@lts.sel.alcatel.de "James Kanze US/ESC 60/3/141 #40763" writes:
  19.  
  20. |> >Again, I think that one should make the distinction (at least in
  21. |> >comp.lang.c) between undefined undefined by the standard, and really
  22. |> >undefined.
  23.  
  24. |> I don't think you'll find it easy to make such a distinction.
  25.  
  26. I have one example which actually occurs in real code.  I need to
  27. declare a buffer (array of unsigned char) with sufficient size *AND*
  28. correct alignment for an arbitrary type.  I normally do this with:
  29.  
  30.     union
  31.     {
  32.         unsigned char       data[ n * sizeof( T ) ] ;
  33.         union MaxAlign      dummyForAlignment ;
  34.     }                   buffer ;
  35.  
  36. where MaxAlign is defined (in a header file) as:
  37.  
  38.     union MaxAlign
  39.     {
  40.         long                l ;
  41.         unsigned long       ul ;
  42.         double              d ;
  43.         long double         ld ;
  44.         void*               pv ;
  45.         struct X*           ps ;
  46.         void              (*pf)() ;
  47.     } ;
  48.  
  49. In practice, I have yet to hear of a machine where the above union
  50. wouldn't guarantee an alignment sufficient for any possible type, but
  51. as far as I can tell, this is not guaranteed by the standard.  (And of
  52. course, if it ever does fail, I'll just add whatever new type is
  53. necessary to make it work.)
  54.  
  55. I consider the code which uses this construct well defined and
  56. completely portable in real life, but it is undefined according to the
  57. standard.
  58.  
  59. I'll admit that there are very few such cases, and most of them go in
  60. the other direction: code that is well defined and legal according to
  61. the standard, but will cause trouble on a large class of machines.
  62. --
  63. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  64. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  65. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  66.                 -- A la recherche d'une activitΘ dans une region francophone
  67.  
  68.